﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using iWare.Wms.Core;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;

namespace iWare.Wms.Application
{
    /// <summary>
    /// 首页
    /// </summary>
    [ApiDescriptionSettings("首页", Name = "LocationView", Order = 100)]
    [Route("api/[Controller]")]
    public class IndexCountService : IIndexCountService, IDynamicApiController, ITransient
    {
        private readonly IRepository<WmsContainer, MasterDbContextLocator> _wmsContainerRep;
        private readonly IRepository<WmsMaterial, MasterDbContextLocator> _wmsMaterialRep;
        private readonly IRepository<WmsMaterialContainer, MasterDbContextLocator> _wmsMaterialContainerRep;
        private readonly IRepository<WmsArea, MasterDbContextLocator> _wmsAreaRep;
        private readonly IRepository<WmsPlace, MasterDbContextLocator> _wmsPlaceRep;
        private readonly IRepository<WmsTask, MasterDbContextLocator> _wmsTaskRep;
        private readonly IRepository<WmsMaterialStock, MasterDbContextLocator> _wmsMaterialStockRep;
        private readonly IRepository<WmsContainerPlace, MasterDbContextLocator> _wmsContainerPlaceRep;


        public IndexCountService(
             IRepository<WmsContainer, MasterDbContextLocator> wmsContainerRep,
            IRepository<WmsMaterial, MasterDbContextLocator> wmsMaterialRep,
            IRepository<WmsMaterialContainer, MasterDbContextLocator> wmsMaterialContainerRep,
            IRepository<WmsArea, MasterDbContextLocator> wmsAreaRep,
            IRepository<WmsPlace, MasterDbContextLocator> wmsPlaceRep,
            IRepository<WmsTask, MasterDbContextLocator> wmsTaskRep,
            IRepository<WmsMaterialStock, MasterDbContextLocator> wmsMaterialStockRep,
            IRepository<WmsContainerPlace, MasterDbContextLocator> wmsContainerPlaceRep
        )
        {
            _wmsContainerRep = wmsContainerRep;
            _wmsMaterialRep = wmsMaterialRep;
            _wmsMaterialContainerRep = wmsMaterialContainerRep;
            _wmsAreaRep = wmsAreaRep;
            _wmsPlaceRep = wmsPlaceRep;
            _wmsTaskRep = wmsTaskRep;
            _wmsMaterialStockRep = wmsMaterialStockRep;
            _wmsContainerPlaceRep = wmsContainerPlaceRep;
        }

        /// <summary>
        /// 获取当日任务执行信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("GetTaskCount")]
        public async Task<TaskCountOtput> GetTaskCount()
        {
            var taskList = await _wmsTaskRep.Where(p => p.CreatedTime >= DateTime.Now.Date).ToListAsync();
            return new TaskCountOtput()
            {
                chukuNum = taskList.Where(n => n.TaskType == Core.Enum.TaskType.CHUKU).Count(),
                rukuNum = taskList.Where(n => n.TaskType == Core.Enum.TaskType.RUKU).Count(),
                yikuNum = taskList.Where(n => n.TaskType == Core.Enum.TaskType.YIKU).Count(),
            };
        }

        /// <summary>
        /// 获取任务执行明细
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[HttpGet("GetTaskDetail")]
        public async Task<List<TaskDetailOtput>> GetTaskDetail([FromQuery] TaskDetailInput input)
        {
            //近七天
            int days = 0;
            if (input.LookType == 1) days = -7;
            else if (input.LookType == 2) days = -30;
            else days = -90;
            var taskList = await _wmsTaskRep.Where(p => p.CreatedTime >= DateTime.Now.Date.AddDays(days)).OrderByDescending(n => n.CreatedTime).ToListAsync();

            var TaskDetailOtputList = new List<TaskDetailOtput>();

            for (int i = 0; i >= days; i--)
            {
                var starttime = DateTime.Now.Date.AddDays(i);
                var endtime = DateTime.Now.Date.AddDays(i + 1);
                var areaTaskList = taskList.Where(p => p.CreatedTime >= starttime && p.CreatedTime <= endtime).ToList();
                var model = new TaskDetailOtput()
                {
                    chukuNum = areaTaskList.Where(n => n.TaskType == Core.Enum.TaskType.CHUKU).Count(),
                    rukuNum = areaTaskList.Where(n => n.TaskType == Core.Enum.TaskType.RUKU).Count(),
                    dateStr = starttime.ToString("MM/dd")
                };
                TaskDetailOtputList.Add(model);
            }
            return TaskDetailOtputList;
        }


        /// <summary>
        /// 库位信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("GetPalceDetail")]
        public async Task<PalceDetailOtput> GetPalceDetailOtput()
        {
            var palceList = await _wmsPlaceRep.Where(p => p.IsDeleted == false).ToListAsync();

            var countNum = palceList.Count;
            var cunhuoNum = palceList.Where(n => n.PlaceStatus == Core.Enum.PlaceStatus.CUNHUO && n.EmptyContainer == YesOrNot.N).Count();
            var emptyNum = palceList.Where(n => n.PlaceStatus == Core.Enum.PlaceStatus.KONGXIAN).Count();
            var emptyContainerNum = palceList.Where(p => p.EmptyContainer == YesOrNot.Y && p.PlaceStatus == Core.Enum.PlaceStatus.CUNHUO).Count();
            //var cunhuoBFB = Convert.ToDecimal(((countNum - emptyNum- emptyContainerNum) / countNum) * 100);
           // var emptyContainerBFB = Convert.ToDecimal(((countNum - cunhuoNum- emptyNum) / countNum) * 100);
            //var emptyBFB = Convert.ToDecimal(((countNum - cunhuoNum- emptyContainerNum) / countNum) * 100);
            return new PalceDetailOtput()
            {
                countNum= countNum,
                cunhuoNum= cunhuoNum,
                emptyNum= emptyNum,
                emptyContainerNum= emptyContainerNum
            };
        }
    }
}
